﻿/**
 @Name：课程信息管理
 @Author：tao
 */

layui.define(['table', 'form'], function (exports) {
  var $ = layui.jquery,
    layer = layui.layer,
    form = layui.form;
  var table = layui.table;

  function myUpdate(obj, setData, callback) {
    //'编辑'按钮的回调
    var tr = $(obj.tr);
    var data = obj.data;
    var dialog = $('#Dialog');

    //弹窗, 包含'yes'和'success'两个回调函数
    layer.open({
      type: 1,
      title: '修改班级信息',
      content: dialog,
      area: ['550px', '350px'],
      btn: ['确定', '取消'],
      resize: false,
      yes: function (index, layero) {
        //弹窗'确定'按钮的回调
        //获取iframe元素的值
        let d = form.val("layuiadmin-form-replys");
        console.log(d)
        layer.close(index);
        callback && callback(d)
      },
      //弹窗弹出后的回调
      success: function (layero, index) {
        //将行的数据传到弹窗
        form.val("layuiadmin-form-replys", {
          class_course_id: data.class_course_id,
          count: data.count
        })
        setCourseId($("select[name='course_id']"), [data])
        setWeek($("select[name='start']"), data.start)
        setWeek($("select[name='last']"), data.last)
        setTime($("select[name='time']"), data.time)
        setDay($("select[name='day']"), data.day)
        $("select[name='course_id']").attr('disabled', true)
        $("input[name='class_course_id']").attr('disabled', true)
        form.render();
      }
    });
  }


  /**
   * 获取多选框的id, 以及需删除的行对象
   * @param {object} delData  选择的所有行数据
   */
  function getDataId(delData) {
    var arr = [];
    for (var i in delData) {
      if (delData[i].checked || !delData[i].hasOwnProperty('checked')) {
        arr.push(delData[i].data.class_course_id)
      } else {
        //删checked为false的行
        delete delData[i]
      }
    }
    return {
      delData,
      arr
    }
  }


  function batchadd(data, callback) {
    var dialog = $('#Dialog');
    var is_must = dialog.find('input[name="is_must"]')[0];
    layer.open({
      type: 1,
      title: '添加班级',
      content: dialog,
      area: ['550px', '350px'],
      btn: ['确定', '取消'],
      resize: false,
      yes: function (index, layero) {
        var data1 = form.val("layuiadmin-form-replys");
        //向服务器发起请求  添加数据
        if (isNull(data1)) {
          layer.close(index)
          callback && callback(data1)
        } else {
          layer.msg('内容不能为空')
        }
      },
      success: function (layero, index) {
        //清空表单
        form.val("layuiadmin-form-replys", {
          class_course_id: '',
          count: 50
        })
        setCourseId($("select[name='course_id']"), data)
        setWeek($("select[name='start']"))
        setWeek($("select[name='last']"), 17)
        setTime($("select[name='time']"))
        setDay($("select[name='day']"))
        $("select[name='course_id']").attr('disabled', false)
        $("input[name='class_course_id']").attr('disabled', false)
        form.render();
      }
    })
  }


  function batchdel(delData, callback) {
    var checkStatus = table.checkStatus('LAY-app-forumreply-list'),
      checkData = checkStatus.data; //得到选中的数据
    if (checkData.length === 0) {
      return layer.msg('请选择数据');
    } else {
      layer.confirm('确定删除吗？', function () {
        callback && callback(getDataId(delData))
      })
    };
  }

  function isNull(obj) {
    for (let item in obj) {
      if (obj[item] === '') {
        return false
      }
    }
    return true
  }

  //设置周数
  function setWeek($dom, value = 1) {
    let str = ''
    for (let i = 1; i <= 17; i++) {
      str += `<option value="${i}" >${i}</option>`
    }
    $dom.html(str)
    form.render('select')
    $dom.find(`option[value='${value}']`).prop("selected", true);
    form.render('select')
  }

  //设置课程号
  function setCourseId($dom, data, value) {
    value = value || data[0].course_id
    console.log(data)
    $dom.html(data.reduce((prev, item) => {
      return prev + `<option value="${item.course_id}">${item.name}(${item.course_id})</option>`
    }, ''))
    form.render('select')
    $dom.find(`option[value='${value}']`).prop("selected", true);
    form.render('select')
  }

  //设置上课时间
  function setTime($dom, value = 0) {
    let time = ["09:00-10:20", "10:40-12:00", "14:00-15:20", "15:30-16:50", "17:00-18:20", "19:00-20:20", "20:30-21:50"];
    $dom.html(time.reduce((prev, item, index) => {
      return prev + `<option value="${index}">${item}</option>`
    }, ''))
    form.render('select')
    $dom.find(`option[value='${value}']`).prop("selected", true);
    form.render('select')
  }

  //设置上课日
  function setDay($dom, value = 1) {
    let day = ["一", "二", "三", "四", "五"];
    $dom.html(day.reduce((prev, item, index) => {
      return prev + `<option value="${index + 1}">星期${item}</option>`
    }, ''))
    form.render('select')
    $dom.find(`option[value='${value}']`).prop("selected", true);
    form.render('select')
  }


  exports('open', {
    getDataId,
    myUpdate,
    batchadd,
    batchdel
  })
});
